package com.xiang.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xiang.async.AsynImageLoader;
import com.xiang.constant.DealBaseInfo;
import com.xiang.seemovies.R;

public class DealListAdapter extends BaseAdapter {
	private AsynImageLoader mImageAsynLoader;
	private LayoutInflater inflater;
	List<DealBaseInfo> dataList;
	
	public DealListAdapter(Context ctx, List<DealBaseInfo> list) {
		inflater = LayoutInflater.from(ctx);
		this.dataList = list;
		mImageAsynLoader = new AsynImageLoader();
	}
	public void resetData( List<DealBaseInfo> list) {
		this.dataList = list;
	}

	@Override
	public int getCount() {
		return dataList.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		DealBaseInfo baseinfo = dataList.get(position);
		ViewHolder holder = null;
		if(convertView==null){
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.deal_list_item, null);
			holder.img = (ImageView) convertView.findViewById(R.id.deal_list_img);
			holder.tittle = (TextView) convertView.findViewById(R.id.deal_list_tittle);
			holder.purchase_count = (TextView) convertView.findViewById(R.id.deal_list_purchase_count);
			holder.current_price = (TextView) convertView.findViewById(R.id.deal_list_current_price);
			
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		Bitmap cachedImage = mImageAsynLoader.loadDrawableFromNet(holder.img, baseinfo.s_image_url); 
		holder.img.setImageBitmap(cachedImage);
		holder.tittle.setText(baseinfo.title);
		holder.purchase_count.setText("购买数："+baseinfo.purchase_count);
		holder.current_price.setText("￥"+baseinfo.current_price);
		return convertView;
	}

	class ViewHolder{
		ImageView img;
		TextView tittle;
		TextView purchase_count;
		TextView current_price;
	}
}
